package com.zx.service.goods.impl;

import com.zx.config.exception.BaseBusinessException;
import com.zx.dao.goods.GoodsImgMapper;
import com.zx.dao.goods.GoodsMapper;
import com.zx.dto.request.goods.GoodsRequestDto;
import com.zx.dto.request.order.OrderDtoRequest;
import com.zx.dto.response.brand.BrandDto;
import com.zx.dto.response.goods.*;
import com.zx.dto.response.specification.SpecificationBrandDto;
import com.zx.enums.GoodsImgTypeEnums;
import com.zx.service.goods.GoodsService;
import com.zx.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunkai
 * @date 2022/7/30 0030 16:30
 * @mood zhou xun
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImgMapper goodsImgMapper;

    @Override
    public List<GoodsDto> queryGoods() {
        List<GoodsDto> goodsDtoLists = goodsMapper.queryGoods();
        Map<Integer, GoodsDto> goodsDtoHashMap =
                goodsDtoLists.stream().collect(Collectors.toMap(GoodsDto::getId, a -> a, (k1, k2) -> k1));
        List<Integer> goodsIds = goodsDtoLists.stream().map(GoodsDto::getId).collect(Collectors.toList());
        List<GoodsImgDto> goodsImgDtoList = goodsImgMapper.queryList(goodsIds, GoodsImgTypeEnums.COVER.getType());
        for (GoodsImgDto goodsImgDto : goodsImgDtoList) {
            GoodsDto goodsDto = goodsDtoHashMap.get(goodsImgDto.getGoodsId());
            if (CollectionUtils.isEmpty(goodsDto.getGoodsImgDtoList())) {
                goodsDto.setGoodsImgDtoList(new ArrayList<>());
            }
            goodsDto.getGoodsImgDtoList().add(goodsImgDto);
        }
        return goodsDtoLists;
    }

    //分页封装
    private GoodsListDtoResult goodsPageList(GoodsRequestDto goodsRequestDto) {
        GoodsListDtoResult goodsListDtoResult = new GoodsListDtoResult();
        goodsListDtoResult.setTotalRows(goodsMapper.queryGoodCount(goodsRequestDto));
        goodsRequestDto.setTotalPage(
                PageUtil.totalPage(goodsListDtoResult.getTotalRows(), goodsRequestDto.getPageSize()));
        goodsListDtoResult.setPageSize(goodsRequestDto.getPageSize());
        goodsListDtoResult.setTotalPage(goodsRequestDto.getTotalPage());
        return goodsListDtoResult;
    }

    @Override
    public GoodsListDtoResult queryGood(GoodsRequestDto goodsRequestDto) {
        Integer index=goodsRequestDto.getPageIndex();
        //数据起始位置
        goodsRequestDto.setPageIndex(
                PageUtil.pageNumber(goodsRequestDto.getPageIndex(), goodsRequestDto.getPageSize()));
        List<GoodsDto> goodsDtos = goodsMapper.queryGood(goodsRequestDto);
        Map<Integer, GoodsDto> goodsDtoHashMap =
                goodsDtos.stream().collect(Collectors.toMap(GoodsDto::getId, a -> a, (k1, k2) -> k1));
        GoodsListDtoResult goodsListDtoResult = goodsPageList(goodsRequestDto);
        goodsListDtoResult.setPageIndex(index);
        if (goodsListDtoResult.getTotalRows()<=0){
            return new GoodsListDtoResult();
        }
        List<Integer> goodsIds = goodsDtos.stream().map(GoodsDto::getId).collect(Collectors.toList());
        List<GoodsDto> goodsDtoList = goodImg(goodsIds, goodsDtos, goodsDtoHashMap);
        goodsListDtoResult.setGoodsDtoList(goodsDtoList);
        return goodsListDtoResult;
    }

    @Override
    public GoodsListDtoResult goodSort(GoodsRequestDto goodsRequestDto) {
        Integer index=goodsRequestDto.getPageIndex();
        //数据起始位置
        goodsRequestDto.setPageIndex(
                PageUtil.pageNumber(goodsRequestDto.getPageIndex(), goodsRequestDto.getPageSize()));
        List<GoodsDto> goodsDtos = goodsMapper.queryGood(goodsRequestDto);
        Map<Integer, GoodsDto> goodsDtoHashMap =
                goodsDtos.stream().collect(Collectors.toMap(GoodsDto::getId, a -> a, (k1, k2) -> k1));
        GoodsListDtoResult goodsListDtoResult = goodsPageList(goodsRequestDto);
        goodsListDtoResult.setPageIndex(index);
        if (goodsListDtoResult.getTotalRows()<=0){
            return new GoodsListDtoResult();
        }
        List<Integer> goodsIds = goodsDtos.stream().map(GoodsDto::getId).collect(Collectors.toList());
        List<GoodsDto> goodsDtoList = goodImg(goodsIds, goodsDtos, goodsDtoHashMap);
        goodsListDtoResult.setGoodsDtoList(goodsDtoList);
        return goodsListDtoResult;
    }

    //查询图片
    private List<GoodsDto> goodImg(List<Integer> goodsIds, List<GoodsDto> goodsDtos, Map<Integer, GoodsDto> goodsDtoHashMap) {
        List<GoodsImgDto> goodsImgDtoList = goodsImgMapper.queryList(goodsIds, GoodsImgTypeEnums.COVER.getType());
        for (GoodsImgDto goodsImgDto : goodsImgDtoList) {
            GoodsDto goodsDto = goodsDtoHashMap.get(goodsImgDto.getGoodsId());
            if (CollectionUtils.isEmpty(goodsDto.getGoodsImgDtoList())) {
                goodsDto.setGoodsImgDtoList(new ArrayList<>());
            }
            goodsDto.getGoodsImgDtoList().add(goodsImgDto);
        }
        return goodsDtos;
    }

    @Override
    public List<GoodsDetailDto> queryGoodShop(List<Integer> id) {
        return goodsMapper.queryGoodShop(id);
    }

    @Override
    public List<GoodsDetailDto> goodShop(OrderDtoRequest orderDtoRequest) {
        return goodsMapper.goodShop(orderDtoRequest);
    }

    @Override
    public GoodsDetailDto queryMerchandiseDetails(Integer id) {
        GoodsDetailDto goodsDto = goodsMapper.queryMerchandiseDetails(id);
        if (ObjectUtils.isEmpty(goodsDto)) {
            return new GoodsDetailDto();
        }
        List<Integer> integerList = new ArrayList<>();  //图片商品id
        Map<Integer, GoodsDetailDto> goodMap = new HashMap<>();//存储图片
        goodMap.put(goodsDto.getId(), goodsDto);
        integerList.add(goodsDto.getId());
        List<GoodsImgDto> goodsImgDtoCarouselList = goodsImgMapper.queryList(integerList, GoodsImgTypeEnums.CAROUSEL.getType());    //展示图
        List<GoodsImgDto> goodsImgDtoParticularsList = goodsImgMapper.queryList(integerList, GoodsImgTypeEnums.PARTICULARS.getType());//详情图
        goodsDto = imgSegment(goodMap, goodsImgDtoCarouselList, goodsImgDtoParticularsList, goodsDto);
        /* for (GoodsImgDto goodsImgDto : goodsImgDtoCarouselList) {       //展示图
            GoodsDetailDto goodsDetailDto = goodMap.get(goodsImgDto.getGoodsId());
            if (CollectionUtils.isEmpty(goodsDetailDto.getGoodsImgDtoList())) {
                goodsDetailDto.setGoodsImgDtoList(new ArrayList<>());
            }
            goodsDto.setGoodsImgDtoList(goodsImgDtoCarouselList);
        }
        for (GoodsImgDto goodsImgDto : goodsImgDtoParticularsList) {    //详情图
            GoodsDetailDto goodsDetailDto = goodMap.get(goodsImgDto.getGoodsId());
            if (CollectionUtils.isEmpty(goodsDetailDto.getGoodsImgDtoList())) {
                goodsDetailDto.setGoodsImgDtoList(new ArrayList<>());
            }
            goodsDto.setGoodsImgDtoParticularsList(goodsImgDtoParticularsList);
        }*/
        //商品id
        GoodsRequestDto goodsRequestDto = new GoodsRequestDto();
        goodsRequestDto.setGid(goodsDto.getId());
        //查询规格
        List<GoodsDetailDto> goodsDetailDtos = goodsMapper.queryGoodDetail(goodsRequestDto);
        Map<Integer, GoodsDetailDto> map =
                goodsDetailDtos.stream().collect(Collectors.toMap(GoodsDetailDto::getSpecificationId, a -> a, (k1, k2) -> k1));
        for (GoodsDetailDto goodsRequestDto1 : goodsDetailDtos) {
            GoodsDetailDto goodsDetailDto = map.get(goodsRequestDto1.getSpecificationValueId());
            if (CollectionUtils.isEmpty(goodsDetailDto.getSpecificationDtoList())) {
                goodsDetailDto.setSpecificationDtoList(new ArrayList<>());
            }
            goodsDto.setSpecificationDtoList(goodsDetailDtos);
        }
        return goodsDto;
    }

    @Override
    public List<Integer> queryGoodsBrandId(String goodName) {
        return goodsMapper.queryGoodsBrandId(goodName);
    }

    public List<SpecificationBrandDto> querySpecificationValue(List<BrandDto> brandDtoList, GoodsRequestDto goodsRequestDto) {
        /*规格*/
        List<SpecificationBrandDto> specificationBrandDtoList = goodsMapper.querySpecification(brandDtoList, goodsRequestDto);
        Map<Integer, SpecificationBrandDto> goodsDtoHashMap =
                specificationBrandDtoList.stream().collect(Collectors.toMap(SpecificationBrandDto::getSpecificationId, a -> a, (k1, k2) -> k1));
        for (SpecificationBrandDto specificationBrandDto : specificationBrandDtoList) {
            if (CollectionUtils.isEmpty(specificationBrandDto.getBrandDtoList())) {
                specificationBrandDto.setBrandDtoList(new ArrayList<>());
            }
            specificationBrandDto.getBrandDtoList().addAll(brandDtoList);
        }
        //规格值
        List<SpecificationBrandDto> specificationBrandDtos = goodsMapper.querySpecificationValue(goodsRequestDto);
        if (specificationBrandDtos.isEmpty()) {
           return new ArrayList<>();
        }
        for (SpecificationBrandDto specificationBrandDtoValue : specificationBrandDtos) {
            SpecificationBrandDto specificationBrandDto = goodsDtoHashMap.get(specificationBrandDtoValue.getSpecificationId());
            if (CollectionUtils.isEmpty(specificationBrandDto.getSpecificationValueDtoList())) {
                specificationBrandDto.setSpecificationValueDtoList(new ArrayList<>());
            }
            specificationBrandDto.getSpecificationValueDtoList().add(specificationBrandDtoValue);
        }
        return specificationBrandDtoList;
    }

    //查询商品封面/详情图
    private GoodsDetailDto imgSegment(Map<Integer, GoodsDetailDto> goodMap, List<GoodsImgDto> goodsImgDtoCarouselList, List<GoodsImgDto> goodsImgDtoParticularsList, GoodsDetailDto goodsDto) {
        for (GoodsImgDto goodsImgDto : goodsImgDtoCarouselList) {       //展示图
            GoodsDetailDto goodsDetailDto = goodMap.get(goodsImgDto.getGoodsId());
            if (CollectionUtils.isEmpty(goodsDetailDto.getGoodsImgDtoList())) {
                goodsDetailDto.setGoodsImgDtoList(new ArrayList<>());
            }
            goodsDto.setGoodsImgDtoList(goodsImgDtoCarouselList);
        }
        for (GoodsImgDto goodsImgDto : goodsImgDtoParticularsList) {    //详情图
            GoodsDetailDto goodsDetailDto = goodMap.get(goodsImgDto.getGoodsId());
            if (CollectionUtils.isEmpty(goodsDetailDto.getGoodsImgDtoList())) {
                goodsDetailDto.setGoodsImgDtoList(new ArrayList<>());
            }
            goodsDto.setGoodsImgDtoParticularsList(goodsImgDtoParticularsList);
        }
        return goodsDto;
    }
}
